import functools
from flask import current_app
from flask import g
from flask import session

from info.models import User
#自定义过滤器


def index_filter(index):
    if index == 0:
        return 'first'
    elif index ==1:
        return 'second'
    elif index ==2:
        return 'third'
    else:
        return ''
#让被装饰的函数的属性不发生变化
#自定义验证登录器
def login_required(f):
    @functools.wraps(f)
    def wrapper(*args,**kwargs):
        user_id = session.get('user_id')
        # print(user_id)
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
            #在请求过程中用来临时存储数据
        g.user = user
        return f(*args,**kwargs)
    # wrapper.__name__ = f.__name__
    return wrapper